package com.IntelligentCommunity.community.controller;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.poi.ExcelUtil;
import com.IntelligentCommunity.common.core.web.controller.BaseController;
import com.IntelligentCommunity.common.core.web.domain.AjaxResult;
import com.IntelligentCommunity.common.core.web.page.TableDataInfo;
import com.IntelligentCommunity.common.log.annotation.Log;
import com.IntelligentCommunity.common.log.enums.BusinessType;
import com.IntelligentCommunity.community.domain.DeployVehicles;
import com.IntelligentCommunity.community.domain.RealVehicles;
import com.IntelligentCommunity.community.service.IRealVehiclesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实有车辆信息Controller
 *
 * @author IntelligentCommunity
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/vehicles")
public class RealVehiclesController extends BaseController
{
    @Autowired
    private IRealVehiclesService realVehiclesService;

    /**
     * 查询实有车辆信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RealVehicles realVehicles)
    {
        startPage();
        List<RealVehicles> list = realVehiclesService.selectRealVehiclesList(realVehicles);
        return getDataTable(list);
    }
    /**
     * 导出实有车辆信息列表
     */
    @Log(title = "实有车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RealVehicles realVehicles)
    {
        List<RealVehicles> list = realVehiclesService.selectRealVehiclesList(realVehicles);
        ExcelUtil<RealVehicles> util = new ExcelUtil<RealVehicles>(RealVehicles.class);
        util.exportExcel(response, list, "实有车辆信息数据");
    }

    /**
     * 导入车辆信息
     */
    @PostMapping( "/import")
    public R handleFileUpload(@RequestParam("file") MultipartFile file) {

        ExcelUtil excelUtil = new ExcelUtil(RealVehicles.class);
        try {
            List<RealVehicles> list = excelUtil.importExcel(file.getInputStream());
            if(CollectionUtils.isEmpty(list)){
                throw new RuntimeException("文件内容为空");
            }
            realVehiclesService.insertRealVehiclesList(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }



    /**
     * 获取实有车辆信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(realVehiclesService.selectRealVehiclesById(id));
    }

    /**
     * 新增实有车辆信息
     */
    @Log(title = "实有车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RealVehicles realVehicles)
    {
        return toAjax(realVehiclesService.insertRealVehicles(realVehicles));
    }

    /**
     * 修改实有车辆信息
     */
    @Log(title = "实有车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RealVehicles realVehicles)
    {
        return toAjax(realVehiclesService.updateRealVehicles(realVehicles));
    }

    /**
     * 删除实有车辆信息
     */
    @Log(title = "实有车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(realVehiclesService.deleteRealVehiclesByIds(ids));
    }

    /**
     * 功能：车辆一键布控
     * @param deployVehicles
     * @return
     */
    @Log(title = "车辆一键布控", businessType = BusinessType.INSERT)
    @PostMapping("vehiclesAdd")
    public R vehiclesAdd(@RequestBody DeployVehicles deployVehicles){
        realVehiclesService.vehiclesAdd(deployVehicles);
        return R.ok();
    }

    /**
     * 功能：车辆一键布控查询
     * @param deployVehicles
     * @return
     */
    @PostMapping("/deployList")
    public TableDataInfo deployList(@RequestBody DeployVehicles deployVehicles){
        startPage();
        List<DeployVehicles> list = realVehiclesService.getDeployList(deployVehicles);
        return getDataTable(list);
    }

    /**
     * 功能：编辑车辆布控
     */
    @Log(title = "编辑车辆布控", businessType = BusinessType.UPDATE)
    @PostMapping("/deployUpdate")
    public AjaxResult deployUpdate(@RequestBody DeployVehicles deployVehicles)
    {
        return toAjax(realVehiclesService.deployUpdate(deployVehicles));
    }

    /**
     * 功能：车辆一键布控 撤控
     */
    @Log(title = "车辆一键布控 撤控", businessType = BusinessType.UPDATE)
    @GetMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestParam("id") Long id)
    {
        return toAjax(realVehiclesService.updateStatus(id));
    }

    /**
     * 功能：车辆一键布控开始
     */
    @Log(title = "车辆一键布控开始", businessType = BusinessType.UPDATE)
    @GetMapping("/start")
    public AjaxResult start(@RequestParam("id") Long id)
    {
        return toAjax(realVehiclesService.start(id));
    }

    /**
     * 功能：车辆一键布控暂停
     */
    @Log(title = "车辆一键布控暂停", businessType = BusinessType.UPDATE)
    @GetMapping("/suspend")
    public AjaxResult suspend(@RequestParam("id") Long id)
    {
        return toAjax(realVehiclesService.suspend(id));
    }

    /**
     * 功能：车辆一键布控结束
     */
    @Log(title = "车辆一键布控结束", businessType = BusinessType.UPDATE)
    @GetMapping("/stop")
    public AjaxResult stop(@RequestParam("id") Long id)
    {
        return toAjax(realVehiclesService.stop(id));
    }

    /**
     * 功能：删除车辆一键布控
     */
    @Log(title = "删除车辆一键布控", businessType = BusinessType.DELETE)
    @GetMapping("/deployDelete")
    public AjaxResult deployDelete(@RequestParam("id") Long id)
    {
        return toAjax(realVehiclesService.deployDelete(id));
    }

    /**
     * 导出车辆一键布控列表
     */
    @Log(title = "导出车辆一键布控列表", businessType = BusinessType.EXPORT)
    @PostMapping("/car/export")
    public void exporte(HttpServletResponse response, DeployVehicles deployVehicles)
    {
        List<DeployVehicles> list = realVehiclesService.getDeployLists(deployVehicles);
        ExcelUtil<DeployVehicles> util = new ExcelUtil<DeployVehicles>(DeployVehicles.class);
        util.exportExcel(response, list, "车辆一键布控");
    }
}
